<?php

namespace app\api\controller\car;

use app\api\controller\Controller;
use app\common\model\car\Car;
use think\facade\Db;
/**
 * 个人中心主页
 */
class Index extends Controller
{
    public function car_trip(){
        // 当前用户信息
        $user = $this->getUser();
        $list = [];
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'trip' => $list
        ]);
    }
    public function get_driveage()
    {
        // 当前用户信息
        $user = $this->getUser();
        $list = Db::name('driver_age')->where('status = 1')->select();
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'list' => $list
        ]);
    }
    public function use_car_info()
    {
        $user = $this->getUser();
        $data = $this->request->post();
        if (empty($data['userCar_id'])) {
            return $this->renderError('请选择车辆');
        }
        $where = array(
            //'a.user_id'  => $user['user_id'],
            'a.car_id'   => $data['userCar_id'],
            'a.category' => 2
        );
        $car = Db::name('car')
            ->alias('a')
            ->field('a.*,b.type_name,c.realname as drivername')
            ->join('jjjshop_car_type b','a.car_type = b.type_id','LEFT')
            ->join('jjjshop_my_driver c','a.driver_id = c.id','LEFT')
            ->where($where)
            ->find();
        $list = array(
            'car_info1'         => $car['car_info1'],
            'car_info2'         => $car['car_info2'],
            'car_info3'         => $car['car_info3'],
            'car_info4'         => $car['car_info4'],
            'addusecar_type'    => $car['type_name'],
            'addusecar_plate'   => $car['car_number'],
            'content'           => $car['content'],
            'product_price'     => $car['price'],
            'product_address'   => $car['address'],
            'product_type'      => $car['brand_id'],
            'userCar_id'        => $car['car_id'],
            'product_journey'   => $car['journey'],
            'product_time'      => $car['p_time'],
            'product_discharge' => $car['discharge'],
            'product_box'       => $car['p_box'],
            'product_transfer'  => $car['transfer'],
            'mobile'  => $car['mobile'],
        );
        return $this->renderSuccess('', [
            'userInfo'  => $user,
            'car'       => $list
        ]);
    }

    //删除车辆
    public function del_car()
    {
        $id = $this->request->param('car_id');
        $model = new Car();
        $res = $model->del($id);
        if($res){
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }

    /*获取车辆*/
    public function get_car()
    {
        $model = new Car();
        $list = $model->where(array('status'=>1,'is_delete'=>0))->paginate(15);
        return $this->renderSuccess('获取成功',['list'=>$list]);
    }
 
    /*车辆详情*/
    public function car_details()
    {
        $id = $this->request->param('car_id');
        $model = new Car();
        $info = $model->where(array("car_id"=>$id))->find();
        return $this->renderSuccess('获取成功',['info'=>$info]);
    }

    public function car_list()
    {
        $model = new Car();
        $list = $model->field("car_info4")->where(array('category'=>1,'is_delete'=>0,'status'=>1))->paginate(10);
        return $this->renderSuccess('获取成功',['list'=>$list]);
    }
 

}